package com.demo.pattern.singleton;

/**
 * @author sourire
 * @version 1.0
 * @date 2020/8/17 16:53
 */
public class SingletonTest04 {
    public static void main(String[] args) {

        SingleTon05 instance = SingleTon05.getInstance();
        SingleTon05 instance2 = SingleTon05.getInstance();
        System.out.println(instance == instance2);
        System.out.println("instance === " + instance.hashCode());
        System.out.println("instance2 === " + instance2.hashCode());
    }
}

//线程安全懒汉式

class SingleTon04 {
    //构造器私有化
    private SingleTon04() {
    }
    //初始化静态常量
    private  static SingleTon04 instance ;
    //对外提供静态的调用方法(加锁 线程安全)
    public static synchronized SingleTon04 getInstance() {
        //在调用的时候才初始化 懒加载
        if(instance == null){
            instance = new SingleTon04();
        }
        return instance;
    }
}
